package com.douqu.game.core.entity.ext.data.challenge;


import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.core.config.VipConfig;
import com.douqu.game.core.entity.Player;
import com.douqu.game.core.entity.ext.data.BaseData;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.CoreUtils;

/**
 * @author wangzhenfei
 *         2017-11-28 16:46
 *         远征副本
 */
public class BossData extends BaseData {


    /**
     * 已经重置的次数
     */
    private int hasChallengeTimes;

    /**
     * 当天总的重置次数
     */
    private int todayTotalChallengeTimes;

    /**
     * 最近一次重置时间
     */
    private long lastChallengeTime;


    private Player player;

    public BossData(Player player) {
        this.player = player;
    }

    @Override
    public void init() {
       VipConfig vipConfig = DataFactory.getInstance().getGameObject(DataFactory.VIP_KEY, player.getVipLevel());
       todayTotalChallengeTimes = vipConfig.bossChallengeTimes;
        hasChallengeTimes = 0;
        lastChallengeTime = DataFactory.currentTime;

    }

    @Override
    public void checkInit() {
//        if(lastChallengeTime == 0){
//            init();
//        }

    }

    @Override
    public void reset() {
        VipConfig vipConfig = DataFactory.getInstance().getGameObject(DataFactory.VIP_KEY, player.getVipLevel());
        todayTotalChallengeTimes = vipConfig.bossChallengeTimes;
        hasChallengeTimes = 0;
    }

    @Override
    public void checkReset() {
        if(!CoreUtils.isTodayByOffset(lastChallengeTime, DataFactory.currentTime, ConstantFactory.DEFAULT_RESET_FRESH_TIMES_HOUR))
            reset();
    }

    @Override
    public void writeTo(ByteBuffer buffer) {

        buffer.writeInt(hasChallengeTimes);
        buffer.writeInt(todayTotalChallengeTimes);
        buffer.writeLong(lastChallengeTime);
        buffer.writeBoolean(false);
    }

    @Override
    public void loadFrom(ByteBuffer buffer) {
        hasChallengeTimes = buffer.readInt();
        todayTotalChallengeTimes = buffer.readInt();
        lastChallengeTime = buffer.readLong();
        buffer.readBoolean();
    }

    public boolean hasRedPointRemind() {
        return isChallengeTimesEnough();
    }

    public void addTodayTotalChallengeTimes(int times){
        todayTotalChallengeTimes += times;
    }

    /**
     * 获取剩余刷新次数
     */
    public int getRemainChallengeTimes(){
        if(hasChallengeTimes > todayTotalChallengeTimes){
            hasChallengeTimes = todayTotalChallengeTimes;
        }
        return todayTotalChallengeTimes - hasChallengeTimes;
    }


    /**
     * 重置挑战次数是否足够
     * @return
     */
    public boolean isChallengeTimesEnough(){
        return getRemainChallengeTimes() > 0;
    }

    /**
     * 挑战次数增加1次
     */
    public void addChallengeTimes(){
        hasChallengeTimes ++;
        lastChallengeTime = DataFactory.currentTime;
    }

    /**
     *  购买完增加总挑战次数
     * @param times
     * @return
     */
    public int addTodayTotalChallengeTime(int times) {
        if(times > 0){
            todayTotalChallengeTimes +=   times;
        }
        return todayTotalChallengeTimes;
    }

    /**
     * 获取已经购买的次数
     * @return
     */
    public int getAlreadyBuyTimes(VipConfig vipConfig){
        int times = todayTotalChallengeTimes - vipConfig.bossChallengeTimes;
        if (times <= 0){
            return 0;
        }
        return times;
    }
    public int getHasChallengeTimes() {
        return hasChallengeTimes;
    }

    public int getTodayTotalChallengeTimes() {
        return todayTotalChallengeTimes;
    }
}
